<!DOCTYPE HTML>
<html>
<head>
    <title>电梯公交后台管理系统</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="fonts/iconfont.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="./login/css/style.css">
</head>
<body>
<div class="body_bj">
    <div class="main_bar">
        <div id="login_form">
            <span class="line-left"></span>
            <span class="line-right"></span>

            <p class="title">登录</p>

            <form id="form_widget">

                <em class="fu-1  iconfont  icon-zhanghao"></em><span class="num">账&nbsp;&nbsp;&nbsp;号</span>
                <em class="fu-3  iconfont  icon-mima"></em><span class="pass">密&nbsp;&nbsp;&nbsp;码</span>
                <br>
                <input  type="text" name="phone"  id="j_phone" class="phone" maxlength="11" onBlur="textBlur(this)" onFocus="textFocus(this)"/><span class="error error5"></span>

                <br>
                <input type="password"  id="box_pass" class="txt"  onBlur="textBlur(this)" onFocus="textFocus(this)"/><span class="error error5"></span>
                <br>
                <a>
                    <input  type="button" value="登录" class="btn" id="subBtn"  onclick="this.style.backgroundColor='#EF7632'">
                </a>
                <br>
                <input id="search_pass_link" value="忘记密码?"  onclick="javascrtpt:window.location.href='pages/findPwd.html'">
            </form>
        </div>
    </div>
</div>
</body>
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/md5.js"></script>
<script type="text/javascript">
    function textFocus(el) {
        if (el.defaultValue == el.value) { el.value = ''; el.style.color = '#333'; }
    }
    function textBlur(el) {
        if (el.value == '') { el.value = el.defaultValue; el.style.color = '#999'; }
    }

    $(function (){

        (function register(){

            $(".reg-box .phone").blur(function(){
                reg=/^1[3|4|5|7|8][0-9]\d{4,8}$/i;//验证手机正则(输入前7位至11位)

                if( $(this).val()==""|| $(this).val()=="请输入您的手机号")
                {
                    $(this).addClass("errorC");
                    $(this).next().html("请输入您的手机号");
                    $(this).next().css("display","block");
                }
                else if($(this).val().length<11)
                {
                    $(this).addClass("errorC");
                    $(this).next().html("手机号长度有误！");
                    $(this).next().css("display","block");
                }
                else if(!reg.test($(".reg-box .phone").val()))
                {
                    $(this).addClass("errorC");
                    $(this).next().html("手机号不存在!");
                    $(this).next().css("display","block");
                }
                else
                {
                    $(this).addClass("checkedN");
                    $(this).removeClass("errorC");
                    $(this).next().empty();
                }
            });
            //密码栏失去焦点
            $(".reg-box .password").blur(function(){
                reg =/^[0-9A-Za-z]{6,12}$/;
                if(!reg.test($(".password").val()))
                {
                    $(this).addClass("errorC");
                    $(this).next().html("格式有误，请输入6~12位的数字、字母！");
                    $(this).next().css("display","block");
                }
                else
                {
                    $(this).removeClass("errorC");
                    $(".reg-box .error3").empty();
                    $(this).addClass("checkedN");
                }
            });

        })();
        //登录页面的提示文字
        //账户输入框失去焦点
        (function login_validate(){
            $("#form_widget #j_phone").blur(function(){
                reg=/^1[3|4|5|8][0-9]\d{4,8}$/i;//验证手机正则(输入前7位至11位)

                if( $(this).val()==""|| $(this).val()=="请输入您的账号")
                {
                    $(this).addClass("errorC");
                    $(this).next().html("账号不能为空！");
                    $(this).next().css("display","block");
                }
                else if($("#form_widget #j_phone").val().length<11)
                {
                    $(this).addClass("errorC");
                    $(this).next().html("账号长度有误！");
                    $(this).next().css("display","block");
                }
                else if(!reg.test($("#form_widget #j_phone").val()))
                {
                    $(this).addClass("errorC");
                    $(this).next().html("账号不存在!");
                    $(this).next().css("display","block");
                }
                else
                {
                    $(this).addClass("checkedN");
                    $(this).removeClass("errorC");
                    $(this).next().empty();
                }
            });
            /*密码输入框失去焦点*/
            $("#form_widget #box_pass").blur(function(){
                reg=/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,12}$/;

                if($(this).val() == "请输入密码"){
                    $(this).addClass("errorC");
                    $(this).next().html("密码不能为空！");
                    $(this).next().css("display","block");

                }else if(!reg.test($("#form_widget #box_pass").val()))
                {
                    $(this).addClass("errorC");
                    $(this).next().html("密码为6~12位的数字、字母！");
                    $(this).next().css("display","block");
                }else
                {
                    $(this).addClass("checkedN");
                    $(this).removeClass("errorC");
                    $(this).next().empty();
                }
            });

        })();

        $("#subBtn").off("click").on("click",function(){
            var telInput = $("#j_phone").val();
            var passInput=hex_md5($("#box_pass").val());

            if(telInput && passInput){
                $.ajax({
                    url: '../login',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        user_tel:telInput,
                        user_pwd:passInput
                    }
                })
                    .done(function(response){
                        console.log(response);
                        if(response.resultCode == 200){
                            var  user= response.user;
                            localStorage.setItem("user_id", response.user.user_id);
                            localStorage.setItem("user_tel", response.user.user_tel);

                            var role_name = response.user.role_name;

                            if(role_name == "超级管理员"){
                                window.location.href="index-a.html";
                            }else if(role_name == "管理员"){
                                window.location.href="index.html";
                            }else if(role_name == "普通用户"){
                                window.location.href="index-c.html";
                            }
                        }else{
                            alert("账号或密码错误，请重新输入！");
                        }
                    })
                    .fail(function(response){
                        console.log("fail",response);
                        if(response.resultCode=='002'){
                            console.log("Phone number is not vaild");
                        }else if(response.resultCode=='001'){
                            console.log("param is not found");
                        }
                    })
            }
        })
    });

    /*清除提示信息*/
    function emptyRegister(){
        $(".reg-box .phone,.reg-box .phonekey,.reg-box .password,.reg-box .email").removeClass("errorC");;
        $(".reg-box .error1,.reg-box .error2,.reg-box .error3,.reg-box .error4").empty();
    }
    function emptyLogin(){
        $(".reg-box .account,.reg-box .admin_pwd,.reg-box .photokey").removeClass("errorC");;
        $(".reg-box .error5,.reg-box .error6,.reg-box .error7").empty();
    }
</script>
</html>